package cn.zhaoyuening.fanrpc.provider.core;

import cn.zhaoyuening.fanrpc.common.beans.RpcRequest;
import cn.zhaoyuening.fanrpc.common.beans.RpcResponce;

import java.lang.reflect.Method;
import java.util.Map;

public class ServiceCallHandler {
    public RpcResponce handleRequest(RpcRequest request, Map<String, Object> rpcServiceMap) {
        RpcResponce responce = new RpcResponce();
        Object service = rpcServiceMap.get(request.getServiceName() + ":" + request.getVersion());
        if (service == null) {
            return null;
        }
        try {
            Method method = service.getClass().getMethod(request.getMethod(), request.getParamTypes());
            Object result = method.invoke(service, request.getParam());
            responce.setResult(result);
            responce.setRespId(request.getReqId());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            responce.setError(e);
        }finally {
            return responce;
        }
    }
}
